##########################################
## "Voters get what they want"          ##
##########################################
## Heinrich, Kobayashi, Long            ##
##########################################

##          
## Substantive effects for interaction models
## Script 07
## June 29, 2017


## Load estimated models
########################
to_get <- list.files(path="output/analysis1_interaction/", pattern="Output", full.names=TRUE)
eff <- vector("list", length(to_get))
for(i in 1:length(to_get))
{
  load(to_get[i])
  
  eff[[i]] <- output$SubstEff
}
eff <- ldply(.data=eff, .fun=function(x) x)


## Print/save effects
#####################
eff_tmp <- cbind(eff[,1:4], round(eff[, 5:ncol(eff)], di=2)) 
write.csv(eff_tmp, "output/tables/A1-Interaction-Effects-Table.csv")



## ME | Hierarchy
#################
eff$HierType <- factor(eff$HierType, levels=rev(c("Security hierarchy", "Economic hierarchy")))
for(j in 1:2)
{
  if(j == 1) eff2 <- subset(eff, NewsSet == "Lowest 40")
  if(j == 2) eff2 <- subset(eff, NewsSet == "Top 40")
  g <- ggplot(data=eff2, aes(x=Hier, ymin=Q025, y=Q500, ymax=Q975))
  g <- g + geom_hline(yintercept=0, size=1.2)
  g <- g + geom_ribbon(alpha=.7, colour=NA, fill="black")
  g <- g + geom_line(size=0.9) + scale_y_continuous(name="Change in total aid/ capita", labels=scales::percent)
  g <- g + facet_wrap( ~ HierType, scales="free_x") + theme_bw()
  g <- g + xlab("Hierarchy") + ggtitle(ifelse(j == 1, "Effect of human rights violations on foreign aid under low news coverage",
                                              "Effect of human rights violations foreign aid under high news coverage"))
  g <- g + theme(axis.text = element_text(size=rel(.8)),
                 panel.grid.major.y = element_line(size=0.25, colour="grey80", 
                                                   linetype="dashed"),
                 panel.grid.major.x = element_blank(),               
                 panel.grid.minor = element_blank(),
                 axis.ticks = element_blank(),
                 strip.text = element_text(size=rel(1.2), hjust=0),
                 strip.background = element_blank(),
                 plot.title = element_text(size=rel(1.45), face="bold", hjust=0))
  ggsave(file=paste0("output/figures/A1-ME-Interaction-", 
                     str_replace_all(string=eff2$NewsSet[1], pattern=" ",
                                     replacement=""), ".pdf"),
         width=10.5, height=5.5)
}
